package cn.com.agree.order.converter;

import cn.com.agree.order.dto.OrderDTO;
import cn.com.agree.order.entity.OrderDetail;
import cn.com.agree.order.enums.ResultEnum;
import cn.com.agree.order.exception.OrderException;
import cn.com.agree.order.form.OrderForm;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Descriptions...
 *
 * @author sun.ys
 * @date 2020/8/23.
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    
    public  static OrderDTO  convert(OrderForm orderForm){
    
        Gson gson = new Gson();
        OrderDTO  orderDTO = new OrderDTO();
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
    
    
        List<OrderDetail> orderDetailList = new ArrayList<>();
        try{
        
            orderDetailList = gson.fromJson(orderForm.getItems(),
                                            new TypeToken<List<OrderDetail>>(){
            
            }.getType());
        
        }catch (Exception e){
            log.error("【json转换】 错误，string={}",orderForm.getItems());
            throw  new OrderException(ResultEnum.PARAM_ERROR);
        }
        
        orderDTO.setOrderDetailList(orderDetailList);
        return  orderDTO;
    }


}
